package com.ruoyi.service.impl;

import com.ruoyi.domain.UserHealthGoals;
import com.ruoyi.mapper.UserHealthGoalsMapper;
import com.ruoyi.service.UserHealthGoalsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserHealthGoalsServiceImpl implements UserHealthGoalsService {

    @Autowired
    private UserHealthGoalsMapper userHealthGoalsMapper;

    // 获取用户最新的运动步数目标
    @Override
    public UserHealthGoals getLatestStepGoal(Long userId) {
        return userHealthGoalsMapper.getLatestStepGoal(userId);
    }

    // 获取用户最新的运动时间目标
    @Override
    public UserHealthGoals getLatestTimeGoal(Long userId) {
        return userHealthGoalsMapper.getLatestTimeGoal(userId);
    }

    // 添加目标
    @Override
    public void addGoal(Long userId, Integer name, Integer targetValue) {
        // 创建一个新的目标对象
        UserHealthGoals healthGoal = new UserHealthGoals();
        healthGoal.setUserId(userId);
        healthGoal.setName(name);
        healthGoal.setTargetValue(targetValue);
        healthGoal.setActualValue(0);  // 初始实际值为0
        healthGoal.setStatus(0);  // 目标状态为未完成
        healthGoal.setDate(new Date());  // 设置目标的时间为当前时间

        // 将目标保存到数据库
        userHealthGoalsMapper.insertGoal(healthGoal);
    }

    // 获取所有未完成的健康目标
    @Override
    public List<UserHealthGoals> getAllActiveGoals() {
        return userHealthGoalsMapper.getAllActiveGoals();
    }

    // 更新目标数据
    @Override
    public void updateHealthGoal(UserHealthGoals healthGoal) {
        userHealthGoalsMapper.updateGoal(healthGoal);
    }
}
